package com.atguigu.service.impl;

import com.atguigu.dao.ContainerDao;
import com.atguigu.dao.impl.ContainerDaoImpl;
import com.atguigu.pojo.Container;
import com.atguigu.pojo.Page;
import com.atguigu.pojo.Server;
import com.atguigu.pojo.Service;
import com.atguigu.service.ContainerService;

import java.util.List;

public class ContainerServiceImpl implements ContainerService {
    ContainerDao containerDao=new ContainerDaoImpl();
    @Override
    public void addContainer(Container container) {
        containerDao.addContainer(container);
    }

    @Override
    public void updateContainer(Container container) {
        containerDao.updateContainer(container);
    }

    @Override
    public void deleteContainerById(String id) {
        containerDao.deleteContainerById(id);
    }

    @Override
    public Container queryContainerById(String id) {
        Container container=containerDao.queryContainerById(id);
        return container;
    }

    @Override
    public List<Container> queryContainersByServerId(String serverId) {
        List<Container> containers=containerDao.queryContainersByServerId(serverId);
        return containers;
    }

    @Override
    public List<Container> queryContainersByName(String containerName) {
        List<Container> containers=containerDao.queryContainersByName(containerName);
        return containers;
    }
    @Override
    public List<Container> queryContainersByServerIdAndContainerName(String serverId, String containerName) {
        List<Container> containers=containerDao.queryContainersByServerIdAndContainerName(serverId,containerName);
        return containers;
    }

    @Override
    public List<Container> queryContainers() {
        List<Container> containers=containerDao.queryContainers();
        return containers;
    }

    @Override
    public Page page(Integer pageNo, Integer pageSize) {
        Page<Container> page=new Page<>();
        page.setPagesize(pageSize);
        Integer pageTotalCount=containerDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount % pageSize >0){
            pageTotal++;//总条数,除以一页条数,大于0,页码数+1
        }
        page.setPageTotal(pageTotal);
        System.out.println("pageNo 原来是 is:"+pageNo);
        if(pageNo<1){//防止用户输入量太大
            pageNo=1;
        }else if(pageNo>pageTotal){
            pageNo=pageTotal;
        }
        page.setPageNo(pageNo);//当前页码
        System.out.println("pageNo 现在是 is:"+pageNo);
        System.out.println("pageTotal is:"+pageTotal);
        //要得到的是page的items
        Integer begin=(page.getPageNo()-1)*pageSize;
        List<Container> items=containerDao.queryForPageItems(begin,pageSize);
        page.setItems(items);
        return page;
    }
}
